Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-doc] Add --asset option to clang-doc #94717

Merged
merged 20 commits into from
Jun 20, 2024

Conversation

PeterChou1
Copy link
Contributor

@PeterChou1 PeterChou1 commented Jun 7, 2024

Adds a new option --asset which allows users to specified the asset folder for the html output of clang-doc

related to: #93928

@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: None (PeterChou1)

Changes

Adds a new option --asset which allows users to specified the asset folder for the html output of clang-doc


Full diff: https://github.com/llvm/llvm-project/pull/94717.diff

1 Files Affected:

  • (modified) clang-tools-extra/clang-doc/tool/ClangDocMain.cpp (+55-18)
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 21b581fa6df2e..df53c46b4a76e 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -81,6 +81,12 @@ static llvm::cl::list<std::string> UserStylesheets(
     llvm::cl::desc("CSS stylesheets to extend the default styles."),
     llvm::cl::cat(ClangDocCategory));
 
+static llvm::cl::opt<std::string>
+    UserAssetPath("asset",
+                  llvm::cl::desc("User supplied asset path for html output to "
+                                 "override the default css and js files"),
+                  llvm::cl::cat(ClangDocCategory));
+
 static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
 Directory where processed files are stored.
 Links to definition locations will only be
@@ -131,12 +137,54 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
 }
 
+void GetAssetFiles(clang::doc::ClangDocContext CDCtx) {
+  std::error_code Code;
+  for (auto DirIt = llvm::sys::fs::directory_iterator(
+                std::string(UserAssetPath), Code),
+            dir_end = llvm::sys::fs::directory_iterator();
+       !Code && DirIt != dir_end; DirIt.increment(Code)) {
+    llvm::SmallString<128> filePath = llvm::SmallString<128>(DirIt->path());
+    if (llvm::sys::fs::is_regular_file(filePath)) {
+      if (filePath.ends_with(".css")) {
+        CDCtx.UserStylesheets.push_back(std::string(filePath));
+      } else if (filePath.ends_with(".js")) {
+        CDCtx.FilesToCopy.push_back(std::string(filePath));
+      }
+    }
+  }
+}
+
+void GetDefaultAssetFiles(const char *Argv0,
+                          clang::doc::ClangDocContext CDCtx) {
+  void *MainAddr = (void *)(intptr_t)GetExecutablePath;
+  std::string ClangDocPath = GetExecutablePath(Argv0, MainAddr);
+  llvm::SmallString<128> NativeClangDocPath;
+  llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
+
+  llvm::SmallString<128> AssetsPath;
+  AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
+  llvm::sys::path::append(AssetsPath, "..", "share", "clang");
+  llvm::SmallString<128> DefaultStylesheet;
+  llvm::sys::path::native(AssetsPath, DefaultStylesheet);
+  llvm::sys::path::append(DefaultStylesheet,
+                          "clang-doc-default-stylesheet.css");
+  llvm::SmallString<128> IndexJS;
+  llvm::sys::path::native(AssetsPath, IndexJS);
+  llvm::sys::path::append(IndexJS, "index.js");
+  CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+                               std::string(DefaultStylesheet));
+  CDCtx.FilesToCopy.emplace_back(IndexJS.str());
+
+  llvm::outs() << "No default asset path found using default asset path: "
+               << AssetsPath << "\n";
+}
+
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
   std::error_code OK;
 
   const char *Overview =
-    R"(Generates documentation from source code and comments.
+      R"(Generates documentation from source code and comments.
 
 Example usage for files without flags (default):
 
@@ -182,23 +230,12 @@ Example usage for a project using a compile commands database:
       {"index.js", "index_json.js"}};
 
   if (Format == "html") {
-    void *MainAddr = (void *)(intptr_t)GetExecutablePath;
-    std::string ClangDocPath = GetExecutablePath(argv[0], MainAddr);
-    llvm::SmallString<128> NativeClangDocPath;
-    llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
-    llvm::SmallString<128> AssetsPath;
-    AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
-    llvm::sys::path::append(AssetsPath, "..", "share", "clang");
-    llvm::SmallString<128> DefaultStylesheet;
-    llvm::sys::path::native(AssetsPath, DefaultStylesheet);
-    llvm::sys::path::append(DefaultStylesheet,
-                            "clang-doc-default-stylesheet.css");
-    llvm::SmallString<128> IndexJS;
-    llvm::sys::path::native(AssetsPath, IndexJS);
-    llvm::sys::path::append(IndexJS, "index.js");
-    CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
-                                 std::string(DefaultStylesheet));
-    CDCtx.FilesToCopy.emplace_back(IndexJS.str());
+    if (!UserAssetPath.empty() &&
+        llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+      GetAssetFiles(CDCtx);
+    } else {
+      GetDefaultAssetFiles(argv[0], CDCtx);
+    }
   }
 
   // Mapping phase

@PeterChou1 PeterChou1 changed the title [clang][clang-doc] Add Assets [clang-doc] Add --asset option to clang-doc Jun 7, 2024
Copy link

github-actions bot commented Jun 7, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@PeterChou1 PeterChou1 force-pushed the clang-doc-add-asset-option branch from d535cb7 to 85581ed Compare June 7, 2024 05:55
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
@@ -131,12 +137,55 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
}

void GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
std::error_code Code;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd use Err.

There isn't a completely consistent practice in Clang-Doc code, but FileErr seems most common. https://llvm.org/docs/ProgrammersManual.html#interoperability-with-std-error-code-and-erroror has a little to say, and the Error handling section on that page may be useful, but we don't have a firm convention.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::error_code Code;
std::error_code Err;
Suggested change
std::error_code Code;
std::error_code FileErr;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still uses Code. please use a naming convention consistent w/ this tool and other parts of LLVM before marking it resolved.

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention, that this needs some kind of test. Please add something simple, like extra RUN: lines in the SingleSource.cpp test, that check the diagnostics emitted when using/not using the --assets flag.

Comment on lines 1 to 2
// RUN: rm -rf %t
// RUN: mkdir %t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: rm -rf %t && mkdir %t

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These still seem to be different steps. combining them is intentional. Actually, do you even need this directory? I don't think you do.

clang-tools-extra/test/clang-doc/single-source-html.cpp Outdated Show resolved Hide resolved
clang-tools-extra/test/clang-doc/single-source-html.cpp Outdated Show resolved Hide resolved
@PeterChou1 PeterChou1 force-pushed the clang-doc-add-asset-option branch from 5cba074 to 94b2979 Compare June 11, 2024 20:39
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, modulo the one nit, and premerge checks passing.

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
@ilovepi ilovepi requested a review from petrhosek June 12, 2024 01:40
std::error_code Code;
llvm::SmallString<128> FilePath(UserAssetPath);
for (DirIterator DirIt = DirIterator(UserAssetPath, Code),
DirEnd = DirIterator();
Copy link
Contributor

@ilovepi ilovepi Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DirEnd = DirIterator();
DirEnd;

This doesn't need to be initialized that way, and appears to be the common pattern in clang and elsewhere when using directory_iterator. Usage should probably be consistent w/ what's used in other clang-tools-extra projects. If nothing else, there's an argument for being consistent w/ clang.

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
Comment on lines 1 to 2
// RUN: rm -rf %t
// RUN: mkdir %t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These still seem to be different steps. combining them is intentional. Actually, do you even need this directory? I don't think you do.

clang-tools-extra/test/clang-doc/single-source-html.cpp Outdated Show resolved Hide resolved
clang-tools-extra/test/clang-doc/single-source-html.cpp Outdated Show resolved Hide resolved
clang-tools-extra/test/clang-doc/single-source-html.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ilovepi ilovepi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modulo a few small nits, this is LGTM. Once those are addressed and CI passes, I can land this for you.

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp Outdated Show resolved Hide resolved
@@ -131,12 +137,55 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
}

void GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
std::error_code Code;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still uses Code. please use a naming convention consistent w/ this tool and other parts of LLVM before marking it resolved.

// CHECK: Using default asset: {{.*}}{{[\\/]}}share{{[\\/]}}clang
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@PeterChou1 PeterChou1 Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pathsep looks for : (or ; on Windows) it doesn't look for \ or /

@PeterChou1 PeterChou1 force-pushed the clang-doc-add-asset-option branch from ca3d880 to a9c8465 Compare June 20, 2024 07:20
@ilovepi ilovepi merged commit 724d903 into llvm:main Jun 20, 2024
7 checks passed
@nico
Copy link
Contributor

nico commented Jun 20, 2024

Looks Like this might break tests: http://45.33.8.238/linux/141118/step_7.txt

Please take a look and revert for now if it takes a while to fix.

@ilovepi
Copy link
Contributor

ilovepi commented Jun 21, 2024

Looks Like this might break tests: http://45.33.8.238/linux/141118/step_7.txt

Please take a look and revert for now if it takes a while to fix.

Sorry for the late revert @nico. I'll have @PeterChou1 investigate before relanding.

@ilovepi
Copy link
Contributor

ilovepi commented Jun 21, 2024

@nico Actually, I probably shouldn't have reverted. It seems like your GN build is incorrect, and isn't copying the asset files into the right place. The patch that updated that landed in #95187. @PeterChou1 is going to add you as a reviewer on the reland in question, but I think the existing premerge and post-submit bots are sufficient to show that this isn't something affecting the CMake build.

ilovepi pushed a commit that referenced this pull request Jun 27, 2024
Reapply #94717
Adds a new option --asset which allows users to specified the asset
folder for the html output of clang-doc.

This patch adds a better test for --asset option + fixes bug where
clang-doc assumes that user supplied js file is assume to be index.js
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
Reapply llvm#94717
Adds a new option --asset which allows users to specified the asset
folder for the html output of clang-doc.

This patch adds a better test for --asset option + fixes bug where
clang-doc assumes that user supplied js file is assume to be index.js
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Adds a new option --asset which allows users to specify the asset
folder for the html output of clang-doc.
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants